package com.shaoshaohuo.app.ui;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.framework.SharedPreferencesHelper;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.view.TopbarView;
import com.shaoshaohuo.app.utils.StringHelper;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

/**
 * 设置支付密码
 * 
 * @author cao
 *
 */
public class PayManageActivity extends BaseActivity implements OnClickListener {

	private TopbarView mTopbarView;
	private EditText edittext_input_pwd;
	private Button button_authcode;
	private EditText edittext_authcode;
	private Button button_next;

	private boolean isHavePasswd;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_input_pay_pwd);
		isHavePasswd = getIntent().getBooleanExtra(ExtraName.isHavePasswd, false);
		initView();
		setUpView();
	}

	private void initView() {
		mTopbarView = (TopbarView) this.findViewById(R.id.topbar);
		edittext_input_pwd = (EditText) findViewById(R.id.edittext_input_pwd);
		button_authcode = (Button) findViewById(R.id.button_authcode);
		edittext_authcode = (EditText) findViewById(R.id.edittext_authcode);
		button_next = (Button) findViewById(R.id.button_next);
	}

	private void setUpView() {
		if (isHavePasswd) {
			mTopbarView.setCenterText("修改支付密码");
		} else {
			mTopbarView.setCenterText("设置支付密码");
		}
		mTopbarView.setLeftView(true, true);

		button_authcode.setOnClickListener(this);
		button_next.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button_authcode:
			getAuthcode();
			break;
		case R.id.button_next:
			commit();
			break;
		}
	}

	private void commit() {
		String password = edittext_input_pwd.getText().toString().trim();
		if (TextUtils.isEmpty(password) || password.length() != 6) {
			showToast("请输入合格的支付密码");
			return;
		}

		String code = edittext_authcode.getText().toString().trim();
		if (TextUtils.isEmpty(code)) {
			showToast("请输入验证码");
			return;
		}

		startLoadingDialog();
		RequestService.getInstance().forgotPwd(this, code, StringHelper.toMD5(password), BaseEntity.class,
				new RequestListener() {

					@Override
					public void onSuccess(int requestCode, BaseEntity resultData) {
						dismissLoadingDialog();
						if (resultData.isOk()) {
							if (isHavePasswd) {
								showToast("支付密码修改成功");
							} else {
								showToast("支付密码设置成功");
							}
							SharedPreferencesHelper.setString("isHavePasswd", "1");
							finish();
						} else {
							showToast(resultData.getMsg());
						}
					}

					@Override
					public void onFailed(int requestCode, Exception error, String msg) {
						dismissLoadingDialog();
						showToast(R.string.please_check_network);
					}
				});

	}

	private void getAuthcode() {

		startLoadingDialog();
		RequestService.getInstance().sendPaySms(this, BaseEntity.class, new RequestListener() {

			@Override
			public void onSuccess(int requestCode, BaseEntity resultData) {
				dismissLoadingDialog();
				if (resultData.isOk()) {
					button_authcode.setEnabled(false);
					mHandler.removeMessages(0);
					mHandler.sendMessageDelayed(mHandler.obtainMessage(0), 1 * 1000);
					showToast("验证码已发送");
				} else {
					showToast(resultData.getMsg());
				}
			}

			@Override
			public void onFailed(int requestCode, Exception error, String msg) {
				dismissLoadingDialog();
				showToast(R.string.please_check_network);
			}
		});
	}

	private static final int DEFAULT_SMS_VERIFYCODE_COUNTDOWN_TIME = 60;
	private int mUpdateCount = DEFAULT_SMS_VERIFYCODE_COUNTDOWN_TIME;
	private final Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (msg.what == 0) {
				--mUpdateCount;
				if (mUpdateCount > 0) {
					button_authcode.setText(String.format("重新发送(%d)", mUpdateCount));
					sendMessageDelayed(obtainMessage(0), 1 * 1000);
					button_authcode.setEnabled(false);
				} else {
					button_authcode.setText("重新发送");
					mUpdateCount = DEFAULT_SMS_VERIFYCODE_COUNTDOWN_TIME;
					button_authcode.setEnabled(true);
				}
			}
		}
	};

}
